package com.gxuwz.Market.business.dao;

import org.springframework.stereotype.Repository;

import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gxuwz.Market.business.entity.ServiceAcademy;
import com.gxuwz.Market.business.entity.ServiceBranch;
import com.gxuwz.Market.business.entity.ServiceClass;
import com.gxuwz.Market.business.entity.ServiceMajor;
import com.gxuwz.Market.business.entity.ServiceStudio;
import com.gxuwz.Market.business.entity.SysRoleRight;
import com.gxuwz.Market.business.entity.SysUser;
import com.gxuwz.core.dao.impl.BaseDaoImpl;
import com.gxuwz.core.pagination.Result;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysUser entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.gxuwz.Market.business.dao.SysUser
 * @author MyEclipse Persistence Tools
 */

@Repository("sysUserDAO")
public class SysUserDAO extends BaseDaoImpl<SysUser>{
	/**
	 * 根据条件查找分页
	 * @param sysUser 模型
	 * @param page 第几页
	 * @param row 长度
	 * @return
	 * @author 李宪乾
	 * @date 2017.9.27
	 */
	@SuppressWarnings("unchecked")
	public Result<SysUser> find(SysUser sysUser, int page, int size){
		String queryString = "from SysUser where 1=1";
		if(null != sysUser.getUserName() && !"".equals(sysUser.getUserName())){			
			queryString = queryString + "and userName like '%"+sysUser.getUserName()+"%'";
		}
		if(null != sysUser.getUserType() && !"".equals(sysUser.getUserType())){
			queryString = queryString +" and userType like '%"+sysUser.getUserType()+"%'";
		} 
		if(null != sysUser.getUserCollege() && !"".equals(sysUser.getUserCollege())){
			queryString = queryString +" and userCollege ='"+sysUser.getUserCollege()+"'";
		} 
		if(null != sysUser.getUserMajor() && !"".equals(sysUser.getUserMajor())){
			queryString = queryString +" and userMajor='"+sysUser.getUserMajor()+"'";
		} 
		if(null != sysUser.getUserStudio() && !"".equals(sysUser.getUserStudio())){
			queryString = queryString +" and userStudio = '"+sysUser.getUserStudio()+"'";
		} 
		if(null != sysUser.getUserClass() && !"".equals(sysUser.getUserClass())){
			queryString = queryString +" and userClass ='"+sysUser.getUserClass()+"'";
		
		}
		int start=(page-1)*size;
		int limit =size;
		return (Result<SysUser> )super.find(queryString, null, null, start, limit);
	}
	
	public List<SysUser> findAll() {
		List<SysUser> sysUserList = null;
		String queryString = "from SysUser where 1=1";
		sysUserList = this.getHibernateTemplate().find(queryString);
		return sysUserList;
	}
	
	public List<SysUser> findAllbyClassName(String classname) {
		
		List<SysUser> sysUserList = null;
		//System.out.print("zhe shi dao"+classname);
		String queryString = "from SysUser s where s.userClass ='"+classname+"' ";
		try{
		sysUserList = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		/*for(int i = 0;i<0;i++){
			sysUserList.get(i).getUserName();
			
		}*/
		return sysUserList;
	}
	/**
	 * 根据id查询单个用户
	 * @param userId
	 * @return
	 */
	public SysUser findByUserId(String userId){
		List<SysUser> sysUserList = null;
		String queryString = "from SysUser where userId ='"+userId+"' ";
		try{
			sysUserList = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return sysUserList.get(0);
	}
	
	public List<SysUser> findByProperty(String property, String value){
		List<SysUser> list = null;
		String queryString = "from SysUser where "+property+" ='"+value+"' ";
		try{
			list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<SysRoleRight> findUserPermissions(String userId) {
		String queryString="select new com.gxuwz.Market.business.entity.SysRoleRight(srr.rightId) " +
				   "from SysUser su,SysUserRole sur,SysRole sr,SysRoleRight srr "+
		           "where su.userId=sur.userId and sur.roleId=sr.roleId and sr.roleId=srr.roleId and su.userId='"+userId+"'";
		return getHibernateTemplate().find(queryString);
	}
	
	/**
	 *查询所有的院系
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */

	public List<ServiceAcademy> findAllAcademy() {
		String hql ="from ServiceAcademy";
		return getHibernateTemplate().find(hql);
	}
	/**
	 *通过学院的Id查询学院的专业
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */

	public List<ServiceMajor> findByAca_Id(String userCollege) {
	  String hql ="from ServiceMajor  where academyId='"+userCollege+"'";
		return getHibernateTemplate().find(hql);
	}
	
	/**
	 *通过专业的ID查询学院的班级
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */

	public List<ServiceClass> findByMajorId(String userMajor) {
		String hql="from ServiceClass where majorId='"+userMajor+"'";
		return getHibernateTemplate().find(hql);
	}
	/**
	 *查询学院的班级
	 * @author LCZ
	 * @date 2018-5-12
	 * @return
	 */
	public List<ServiceClass> findClass() {
		String hql="from ServiceClass";
		return getHibernateTemplate().find(hql);
	}
	
	//findUser
	public List<SysUser> findUser(String userId){
		List<SysUser> list = null;
		String queryString = "from SysUser where userId ='"+userId+"' ";
		try{
			list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}
	
	/**
	 *查找研究室
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */

	public List<ServiceStudio> findStudio() {
	 String hql= "from  ServiceStudio";
		return getHibernateTemplate().find(hql);
	}

	/**
	 *查询用户信息
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */	
	public Result findUser(int page,int size) {
		String hql ="select new Map(s1.id as id,s1.userId as userId,s1.userName as userName,s1.userSex as userSex, s1.userType as userType, s1.userPhone as userPhone, s2.academyName as academyName, s3.majorName as majorName,s4.className as className)"
				+ "from  SysUser as s1,ServiceAcademy as s2,ServiceMajor as s3, ServiceClass as s4 "
				+ "where s1.userCollege=s2.academyId and s1.userMajor =s3.majorId  "
				+ "and s1.userClass = s4.classId  "
				+ "and s2.academyId =s3.academyId  "
				+ "and  s3.majorId =s4.majorId ";
		  int start=(page-1)*size;
		  int limit =size;
		return find(hql, null, null, start, limit);
	}
	
	
	/**
	 * 通过学院的Id查找学院
	 * @author 黄森南
	 * @date 2018-1-29
	 * @return
	 */

	public ServiceAcademy findAcayId(String academyId) {
		String hql ="from ServiceAcademy where academyId='"+academyId+"'";
		return (ServiceAcademy) getHibernateTemplate().find(hql).get(0);
	}

	/**
	 * 通过专业的Id查找用户的专业
	 * @author 黄森南
	 * @date 2018-1-25
	 * @return
	 */
	public ServiceMajor findMajById(String majorId) {
		String hql ="from  ServiceMajor where  majorId='"+majorId+"'";
		return (ServiceMajor) getHibernateTemplate().find(hql).get(0);
	}
	
	/**
	 * 根据学院名称、专业名称、班级名称查找用户信息
	 * @author 黄灵燕
	 * @param academyName
	 * @param majorClass
	 * @param ClassName
	 * @return
	 */
	public List<SysUser> findSysUserByInfo(String academyName, String majorName, String ClassName ,String StudioName){
		List<SysUser> sysUserList = null;		
		String hql = "from SysUser where 1 = 1";
		if(academyName != null && !"".equals(academyName)){			
			hql +=" and userCollege='"+academyName+"'";
		}
		if(majorName != null && !"".equals(majorName)){
			 hql +=" and userMajor='"+majorName+"'";			
		}
		if(ClassName != null && !"".equals(ClassName)){			
			 hql  +=" and userClass='"+ClassName+"'";
		}
		if(StudioName != null && !"".equals(StudioName)){
			hql +=" and userStudio = '"+StudioName+"'";
		}
		sysUserList = this.getHibernateTemplate().find(hql);
		return sysUserList;
		
	}
	
	/**
	 * 通过班级编号查找班级信息
	 * @param classId
	 * @return
	 */
	public ServiceClass findClassById(String classId){
		String hql="from ServiceClass where classId='"+classId+"'";
		return (ServiceClass)getHibernateTemplate().find(hql).get(0);
	}
	
	public ServiceStudio findStudioByStudioId(String StudioId){
		String hql="from ServiceStudio where studioId='"+StudioId+"'";
		return (ServiceStudio)getHibernateTemplate().find(hql).get(0);
	}
	
	/**
	 * 根据分类查找用户
	 * @param branchList
	 * @return
	 */
	public List<SysUser> findByBranch(List<ServiceBranch> branchList){
		List<SysUser> list = null;
		StringBuffer queryString = new StringBuffer("from SysUser where 1=2 ");
		if(branchList != null && branchList.size()>0){
			for(ServiceBranch branch: branchList){
				switch (branch.getBranchType()) {
				case 0:
					queryString.append(" or userId='");
					queryString.append(branch.getSonId());
					queryString.append("' ");
					break;
				case 1:
					queryString.append(" or userClass='");
					queryString.append(branch.getSonKey());
					queryString.append("' ");
					break;
				case 2:
					queryString.append(" or userMajor='");
					queryString.append(branch.getSonKey());
					queryString.append("' ");
					break;
				case 3:
					queryString.append(" or userCollege='");
					queryString.append(branch.getSonKey());
					queryString.append("' ");
					break;
				case 4:
					queryString.append("or userStudio='");
					queryString.append(branch.getSonKey());
					queryString.append("' ");
					break;

				default:
					break;
				}
			}
		}
		
		try{
			list = this.getHibernateTemplate().find(queryString.toString());
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}
	
}